package com.boot.main.service.impl;

import com.boot.main.entity.BcomCategory;
import com.boot.main.entity.BcomComment;
import com.boot.main.mapper.BcomCommentMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.ICommentService;
import com.boot.main.utils.IsNotEmptyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentService implements ICommentService {
    @Resource
    private BcomCommentMapper commentMapper;

    @Override
    public ResponseData getComments(Integer page, Integer limit){
        List<BcomComment> items = commentMapper.getComments((page-1)*limit, limit);
        Integer count = commentMapper.getTotalCount();
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取评论成功", map);
    }

    @Override
    public ResponseData getCommentsList(){
        List<BcomComment> items = commentMapper.getCommentsList();
        return ResponseDataUtil.success("获取评论成功", items);
    }

    @Override
    public ResponseData getCommentsByPostId(Long post_id){
        List<BcomComment> items = commentMapper.getCommentsByPostId(post_id);
        return ResponseDataUtil.success("获取评论成功", items);
    }

    @Override
    public ResponseData saveComment(BcomComment comment){
        comment.setCreate_time(new Date().getTime());
        comment.setStatus(0);
        Integer num = commentMapper.saveComment(comment);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("保存评论失败", null);
        }
        return ResponseDataUtil.success("保存评论成功", null);
    }

    @Override
    public ResponseData deleteComment(Long id){
        Integer num = commentMapper.deleteComment(id);
        return ResponseDataUtil.success("删除评论成功", null);
    }
}
